package org.aplombh.java.leetcode.all;

import java.util.Arrays;
import java.util.HashSet;

public class _36有效的数独 {
}

class Solution36_1 {
    public boolean isValidSudoku(char[][] board) {

        HashSet<Character> row = new HashSet<>();
        HashSet<Character> col = new HashSet<>();
        HashSet<Character> min = new HashSet<>();


        for (int i = 0; i < 9; i++) {
            row.clear();
            col.clear();
            min.clear();
            for (int j = 0; j < 9; j++) {

                // row
                if (row.contains(board[i][j])) return false;
                if (board[i][j] != '.') row.add(board[i][j]);

                // col
                if (col.contains(board[j][i])) return false;
                if (board[j][i] != '.') col.add(board[j][i]);

                // min
                int r = (i / 3) * 3 + j / 3;
                int c = (i % 3) * 3 + j % 3;
                if (min.contains(board[r][c])) return false;
                if (board[r][c] != '.') min.add(board[r][c]);
            }
        }
        return true;
    }
}

class Solution36_2 {
    public boolean isValidSudoku(char[][] board) {

        boolean[] row = new boolean[9];
        boolean[] col = new boolean[9];
        boolean[] min = new boolean[9];


        for (int i = 0; i < 9; i++) {
            Arrays.fill(row, false);
            Arrays.fill(col, false);
            Arrays.fill(min, false);
            for (int j = 0; j < 9; j++) {

                // row
                if (board[i][j] != '.') {
                    if (row[board[i][j] - '1']) return false;
                    row[board[i][j] - '1'] = true;
                }

                // col
                if (board[j][i] != '.') {
                    if (col[board[j][i] - '1']) return false;
                    col[board[j][i] - '1'] = true;
                }


                // min
                int r = (i / 3) * 3 + j / 3;
                int c = (i % 3) * 3 + j % 3;

                if (board[r][c] != '.') {
                    if (min[board[r][c] - '1']) return false;
                    min[board[r][c] - '1'] = true;
                }
            }
        }
        return true;
    }
}